package cn.t.repository.impl.order;

import cn.t.converter.order.WorkOrderDetailConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.WorkOrderDetailDO;
import cn.t.persistence.entity.order.WorkOrderDetailPO;
import cn.t.persistence.mapper.order.WorkOrderDetailMapper;
import cn.t.repository.order.WorkOrderDetailRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单明细表(WorkOrderDetail)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:11
 */
@Slf4j
@Repository
@AllArgsConstructor
public class WorkOrderDetailRepositoryImpl extends RepositoryImpl<WorkOrderDetailMapper, WorkOrderDetailPO, WorkOrderDetailConverter, WorkOrderDetailDO> implements WorkOrderDetailRepository {


    @Override
    public List<WorkOrderDetailDO> findByModel(WorkOrderDetailDO workOrderDetailDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<WorkOrderDetailPO> list = getWrapper(workOrderDetailDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<WorkOrderDetailPO> getWrapper(WorkOrderDetailDO workOrderDetailDO) {
        LambdaQueryChainWrapper<WorkOrderDetailPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getWorkOrderSn())) {
            wrapper.eq(WorkOrderDetailPO::getWorkOrderSn, workOrderDetailDO.getWorkOrderSn());
        }
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getSkuCode())) {
            wrapper.eq(WorkOrderDetailPO::getSkuCode, workOrderDetailDO.getSkuCode());
        }
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getSkuName())) {
            wrapper.like(WorkOrderDetailPO::getSkuName, workOrderDetailDO.getSkuName());
        }
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getPlanQty())) {
            wrapper.eq(WorkOrderDetailPO::getPlanQty, workOrderDetailDO.getPlanQty());
        }
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getActQty())) {
            wrapper.eq(WorkOrderDetailPO::getActQty, workOrderDetailDO.getActQty());
        }
        if (!ObjectUtils.isEmpty(workOrderDetailDO.getStatus())) {
            wrapper.eq(WorkOrderDetailPO::getStatus, workOrderDetailDO.getStatus());
        }
        if (StringUtils.hasText(workOrderDetailDO.getKeyword())) {
            wrapper.like(WorkOrderDetailPO::getSkuCode, workOrderDetailDO.getKeyword()).or()
                    .like(WorkOrderDetailPO::getSkuName, workOrderDetailDO.getKeyword()).or()
                    .like(WorkOrderDetailPO::getWorkOrderSn, workOrderDetailDO.getKeyword());
        }

        wrapper.in(!CollectionUtils.isEmpty(workOrderDetailDO.getWorkOrderSns()), WorkOrderDetailPO::getWorkOrderSn, workOrderDetailDO.getWorkOrderSns());
        return wrapper;
    }

    @Override
    public long count(WorkOrderDetailDO workOrderDetailDO) {
        return getWrapper(workOrderDetailDO).count();
    }

    @Override
    public Boolean updateBatchList(List<WorkOrderDetailDO> exceptionLogList) {
        if (CollectionUtils.isEmpty(exceptionLogList)) {
            return false;
        }
        return updateBatchById(exceptionLogList.stream().map(this.converter::model2Po).collect(Collectors.toList()));
    }

    @Override
    public Boolean addBatch(List<WorkOrderDetailDO> workOrderDetailDOS) {
        if (CollectionUtils.isEmpty(workOrderDetailDOS)) {
            return false;
        }

        return this.saveBatch(workOrderDetailDOS.stream().map(this.converter::model2Po).collect(Collectors.toList()));
    }

    @Override
    public Boolean conditionUpdate(String workOrderSn, WorkOrderDetailDO workOrderDetailDO) {
        LambdaUpdateChainWrapper<WorkOrderDetailPO> updateChainWrapper = lambdaUpdate().eq(WorkOrderDetailPO::getWorkOrderSn, workOrderSn)
                .set(StringUtils.hasText(workOrderDetailDO.getCreator()), WorkOrderDetailPO::getCreator, workOrderDetailDO.getCreator());

        return this.update(updateChainWrapper);
    }


}
